package com.accompany.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

/**
 * @author: Dc
 * @Description 代码生成
 * @Date: 2024-12-26 14:03
 * @Version 1.0
 **/
public class CodeGenerator {
    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/apany-db?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&rewriteBatchedStatements=true", "root", "123456dc")
                .globalConfig(builder -> {
                    // 设置作者
                    builder.author("DC")
                            // 开启 swagger 模式
                            .enableSwagger()
                            // 指定输出目录
                            .outputDir("E:\\project\\Java\\accompany-server\\src\\main\\java");
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        // 设置父包名
                        builder.parent("com.accompany")
                                // 设置父包模块名
                                .moduleName(null)
                                .entity("entity")
                                .service("service")
                                .serviceImpl("service.impl")
                                .controller("controller")
                                .mapper("dao")
                                // 设置mapperXml生成路径
                                .pathInfo(Collections.singletonMap(OutputFile.xml, "E:\\project\\Java\\accompany-server\\src\\main\\resources\\dao"))
                )
                .strategyConfig(builder ->
                        // 设置需要生成的表名
                        builder.addInclude("games","masters","skill_tags","master_tags","chat_sessions","chat_messages","orders","wallets","transactions","favorites")
                                .entityBuilder().enableLombok()
                                .serviceBuilder().formatServiceFileName("%sService")
                                .controllerBuilder().enableHyphenStyle()
                                // 设置过滤表前缀
//                                .addTablePrefix("t_", "c_")
                )
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
